package com.yitongmao.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.yitongmao.AppConstancts;
import com.yitongmao.bean.ClassItemRightBean;
import com.yitongmao.bean.LoginHuancunBean;
import com.yitongmao.bean.MyBean;
import com.yitongmao.common.CallBackListener;
import com.yitongmao.core.cache.ACache;
import com.yitongmao.core.network.service.BaseService;
import com.yitongmao.util.SignUtil;
import com.yitongmao.util.ToastUtils;
import com.yitongmao.yitmfruit.R;

import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;

/*
* 作者：梁伟航 on 2017/7/20 17:42
* 类的用途：
*/
public class ClassRightNetWorkOnclickAdapter extends BaseAdapter{
    private Context context;
    private List<ClassItemRightBean.ClassListBean> popularList;
    private int loginId1;

    public ClassRightNetWorkOnclickAdapter(Context context, List<ClassItemRightBean.ClassListBean> popularList) {
        this.context=context;
        this.popularList=popularList;
    }


    @Override
    public int getCount() {
        return popularList.size();
    }

    @Override
    public Object getItem(int position) {
        return popularList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView=View.inflate(context, R.layout.classfyrightitem,null);
            viewHolder.homelistTitle = (TextView) convertView.findViewById(R.id.homelistTitle);
            viewHolder.homelistBody = (TextView) convertView.findViewById(R.id.homelistBody);
            viewHolder.homelistprice = (TextView) convertView.findViewById(R.id.homelistprice);
            viewHolder.homelistgouwu = (ImageView) convertView.findViewById(R.id.homelistgouwu);
            viewHolder.homelistimage = (ImageView) convertView.findViewById(R.id.homelistimage);
            convertView.setTag(viewHolder);//绑定ViewHolder对象
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.homelistgouwu.setOnClickListener(new View.OnClickListener() {



            @Override
            public void onClick(View v) {
                Gson gson = new Gson();
                String loginId = ACache.get(context).getAsString("loginId");
                if (loginId != null && popularList != null) {
                    LoginHuancunBean loginHuancunBean = gson.fromJson(loginId, LoginHuancunBean.class);
                    int userId = loginHuancunBean.userInfo.id;
                    int goodsId = popularList.get(position).id;
                    loginId1 = loginHuancunBean.userInfo.loginId;
                    setNetWork(userId, goodsId);
                }
            }
        });
        viewHolder.homelistTitle.setText(popularList.get(position).goodsName);
        viewHolder.homelistBody.setText(popularList.get(position).describes);
        viewHolder.homelistprice.setText(""+popularList.get(position).retailPrice);
        Glide.with(context).load(popularList.get(position).thumb).into(viewHolder.homelistimage);
        return convertView;
    }
    //加入购物车
    public void setNetWork(int userId,int goodsId ) {
        Map params = new TreeMap();
        String timestamp = String.valueOf(System.currentTimeMillis());
        String nonce = String.valueOf(new Random().nextInt());
        params.put("userId",String.valueOf(userId));
        params.put("goodsId",String.valueOf(goodsId));
        params.put("num","1");
        params.put("spec","0");
        params.put("timestamp", timestamp);
        params.put("nonce", nonce);
        params.put("signLogin", SignUtil.signLogin(nonce, timestamp));
        params.put("loginId", String.valueOf(loginId1));
        params.put("sign", SignUtil.sign(params)); //这里也需要时完整的url
        BaseService.getInstance().simplePostRequest(AppConstancts.Net.addShoppingUrl, params, new CallBackListener() {
            @Override
            public void onStart() {
            }
            @Override
            public void onSuccess(Object result) {
                Log.e("onSuccess",result.toString());
                String string = (String) result;
                Gson gson = new Gson();
                MyBean myBean = gson.fromJson(string, MyBean.class);
                if (myBean.result.state.equals("1")){
                    ToastUtils.showShort(context,"添加成功");
                }else {
                    ToastUtils.showShort(context,""+myBean.result.message);
                }
            }
            @Override
            public void onFail(Object result) {
                System.out.print("失败");
            }
        });
    }
    class ViewHolder {
        public TextView homelistTitle;
        public TextView homelistBody;
        public TextView homelistprice;
        public ImageView homelistgouwu;
        public ImageView homelistimage;
    }
}
